package leeon.forpeddy.htmlparse;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;

import leeon.util.HtmlUtil;

public class GradeHtmlParser {
	
	private String no = null;
	private String name = null;
	private String major = null;
	
	private String score = null;
	private String credit = null;
	private String over70 = null;
	private String english = null;
	private String article = null;
	
	private String inTime = null;
	private String finishTime = null;
	
	private String collageEnglish = null;
	private String collageComputer = null;
	
	private String dgreeTest1 = null;
	private String dgreeTest2 = null;
	
	private boolean avail = true;
	private String checkResult1 = null;
	private String checkResult2 = null;
	
	
	public GradeHtmlParser(String file) throws IOException {
		//读取
		BufferedReader br = new BufferedReader(new FileReader(file));
		StringBuffer sb = new StringBuffer();
		String s = null;
		while ((s = br.readLine()) != null) {
			sb.append(s);
		}
		br.close();
		
		//查找赋值
		String html = sb.toString();
		if (html.indexOf("没有这个学生") != -1) {
			avail = false;
			return;
		}
		
		no = HtmlUtil.findTag(html, "学号：", "</");
		name = HtmlUtil.findTag(html, "姓名：", "</");
		major = HtmlUtil.findTag(html, "学科/专业：", "</");
		
		score = HtmlUtil.findTag(html, "平均分：", "</");
		credit = HtmlUtil.findTag(html, "合格学分：", "</");
		if (no.startsWith("6")) {
			over70 = HtmlUtil.findTag(html, "是否每门功课都在70分以上：", "</");
		} else {
			over70 = HtmlUtil.findTag(html, "是否每门课都在70分以上：", "</");
		}
		english = HtmlUtil.findTag(html, "英语等级：", "<br>");
		article = HtmlUtil.findTag(html, "<TD colspan=\"9\" rowspan=\"2\" style=\"border: 1px solid #000000\" bordercolor=\"#000000\" align=\"center\"><FONT size=\"2\">", "</");
		if (article != null) article = HtmlUtil.findTag(article, "成绩:");
	
		inTime = HtmlUtil.findTag(html, "录取时间：", "</");
		finishTime = HtmlUtil.findTag(html, "大专毕业时间：", "</");
		
		String[] r = HtmlUtil.findTr(html, "大学英语B(统考)");
		if (r == null) collageEnglish = "页面有误[无大学英语B(统考)栏]";
		else if (r.length == 1) collageEnglish = "无成绩";
		else if (r.length == 2) collageEnglish = r[1];
		else if (r.length == 3) collageEnglish = "重修:" + r[2];
		else if (r.length == 4) collageEnglish = "重学:" + r[3];
		
		r = HtmlUtil.findTr(html, "计算机应用基础(统考)");
		if (r == null) collageComputer = "页面有误[无计算机应用基础(统考)栏]";
		else if (r.length == 1) collageComputer = "无成绩";
		else if (r.length == 2) collageComputer = r[1];
		else if (r.length == 3) collageComputer = "重修:" + r[2];
		else if (r.length == 4) collageComputer = "重学:" + r[3];
		
		
		if (no.startsWith("6")) {

			int start = html.indexOf("学位考试成绩");
			if (start != -1) {
				String text = HtmlUtil.findText(html, "学位考", start + 20);
				if (text != null) {
					r = HtmlUtil.findTr(html, text);
					if (r == null) dgreeTest1 = text + ":页面有误[无学位考成绩栏1]";
					else if (r.length == 7) {
						if (!"".equals(r[6]))
							dgreeTest1 = text + ":" + r[6];
						else
							dgreeTest1 = text + ":无成绩";
					} else if (r.length == 8) {
						if (!"".equals(r[7]))
							dgreeTest1 = text + ":重修:" + r[7];
						else if (!"".equals(r[6]))
							dgreeTest1 = text + ":" + r[6];
						else
							dgreeTest1 = text + ":无成绩";
						
					} else if (r.length >= 9) {
						if (!"".equals(r[8]))
							dgreeTest1 = text + ":重学:" + r[8];
						else if (!"".equals(r[7]))
							dgreeTest1 = text + ":重修:" + r[7];
						else if (!"".equals(r[6]))
							dgreeTest1 = text + ":" + r[6];
						else
							dgreeTest1 = text + ":无成绩";
						
						
					} else dgreeTest1 = text + ":页面有误[无学位考成绩栏1]";
					
					
					start = html.indexOf(text);
					text = HtmlUtil.findText(html, "学位考", start + text.length() + 20);
					if (text != null) {
						r = HtmlUtil.findTr(html, text);
						if (r == null) dgreeTest2 = text + ":页面有误[无学位考成绩栏2]";
						else if (r.length == 7) {
							if (!"".equals(r[6]))
								dgreeTest2 = text + ":" + r[6];
							else
								dgreeTest2 = text + ":无成绩";
						} else if (r.length == 8) {
							if (!"".equals(r[7]))
								dgreeTest2 = text + ":重修:" + r[7];
							else if (!"".equals(r[6]))
								dgreeTest2 = text + ":" + r[6];
							else
								dgreeTest2 = text + ":无成绩";
							
						} else if (r.length >= 9) {
							if (!"".equals(r[8]))
								dgreeTest2 = text + ":重学:" + r[8];
							else if (!"".equals(r[7]))
								dgreeTest2 = text + ":重修:" + r[7];
							else if (!"".equals(r[6]))
								dgreeTest2 = text + ":" + r[6];
							else
								dgreeTest2 = text + ":无成绩";
							
						} else dgreeTest2 = text + ":页面有误[无学位考成绩栏2]";
						
					} else {
						dgreeTest2 = "页面有误[无学位考栏2]";
					}
					
				} else {
					dgreeTest1 = "页面有误[无学位考栏1]";
					dgreeTest2 = "页面有误[无学位考栏2]";
				}
			} else {
				dgreeTest1 = "页面有误[无学位考试成绩栏]";
				dgreeTest2 = "页面有误[无学位考试成绩栏]";
			}
			
			
			//论文
			start = html.indexOf("题目");
			if (start != -1) {
				String text = HtmlUtil.findText(html, "学分", start + 20);
				if (text != null) {
					r = HtmlUtil.findTr(html, text, start + 20);
					if (r != null && r.length == 13) article = r[12];
				}
			}
			
			
			start = html.indexOf("重学题目");
			if (start != -1) {
				String text = HtmlUtil.findText(html, "学分", start + 20);
				if (text != null) {
					r = HtmlUtil.findTr(html, text, start + 20);
					if (r != null && r.length == 13) article = "论文重修:" + r[12];
				}
			}
			
			
		} else if (no.startsWith("7") && no.charAt(8) == '0') {
			
			int start = html.indexOf("学位考试和统考成绩");
			if (start != -1) {
				String text = HtmlUtil.findText(html, "学位考", start + 20);
				if (text != null) {
					r = HtmlUtil.findTr(html, text);
					if (r == null) dgreeTest1 = text + ":页面有误[无学位考成绩栏1]";
					else if (r.length == 1) dgreeTest1 = text + ":无成绩";
					else if (r.length == 2) dgreeTest1 = text + ":" + r[1];
					else if (r.length == 3) dgreeTest1 = text + ":重修:" + r[2];
					else if (r.length == 4) dgreeTest1 = text + ":重学:" + r[3];
					
					start = html.indexOf(text);
					text = HtmlUtil.findText(html, "学位考", start + text.length() + 20);
					if (text != null) {
						r = HtmlUtil.findTr(html, text);
						if (r == null) dgreeTest1 = text + ":页面有误[无学位考成绩栏2]";
						else if (r.length == 1) dgreeTest2 = text + ":无成绩";
						else if (r.length == 2) dgreeTest2 = text + ":" + r[1];
						else if (r.length == 3) dgreeTest2 = text + ":重修:" + r[2];
						else if (r.length == 4) dgreeTest2 = text + ":重学:" + r[3];
					} else {
						dgreeTest1 = "页面有误[无学位考栏2]";
					}
					
				} else {
					dgreeTest1 = "页面有误[无学位考栏1]";
					dgreeTest2 = "页面有误[无学位考栏2]";
				}
			} else {
				dgreeTest1 = "页面有误[无学位考试和统考成绩栏]";
				dgreeTest2 = "页面有误[无学位考试和统考成绩栏]";
			}
		} else if (no.startsWith("7") && (no.charAt(8) == '2' || no.charAt(8) == '1')) {
			
		}
	}
	
	public String toString() {
		return "no:" + no +
				", name:" + name +
				", major:" + major + 
				", score:" + score +
				", credit:" + credit +
				", over70:" + over70 +
				", english:" + english +
				", article:" + article +
				", inTime:" + inTime +
				", finishTime:" + finishTime +
				", collageComputer:" + collageComputer +
				", collageEnglish:" + collageEnglish +
				", dgreeTest1:" + dgreeTest1 +
				", dgreeTest2:" + dgreeTest2;
	}
	
	public static void main (String[] as) throws IOException, ParseException {
		//GradeHtmlParser ghp = new GradeHtmlParser("D:\\workspace\\forpeddy\\misc\\704902060058.html");
		GradeHtmlParser ghp = new GradeHtmlParser("D:\\project\\eurospace\\forpeddy\\misc\\6032222035.html");
		System.out.println(ghp);
	}

	public String getArticle() {
		return article;
	}

	public void setArticle(String article) {
		this.article = article;
	}

	public String getCollageComputer() {
		return collageComputer;
	}

	public void setCollageComputer(String collageComputer) {
		this.collageComputer = collageComputer;
	}

	public String getCollageEnglish() {
		return collageEnglish;
	}

	public void setCollageEnglish(String collageEnglish) {
		this.collageEnglish = collageEnglish;
	}

	public String getCredit() {
		return credit;
	}

	public void setCredit(String credit) {
		this.credit = credit;
	}

	public String getDgreeTest1() {
		return dgreeTest1;
	}

	public void setDgreeTest1(String dgreeTest1) {
		this.dgreeTest1 = dgreeTest1;
	}

	public String getDgreeTest2() {
		return dgreeTest2;
	}

	public void setDgreeTest2(String dgreeTest2) {
		this.dgreeTest2 = dgreeTest2;
	}

	public String getEnglish() {
		return english;
	}

	public void setEnglish(String english) {
		this.english = english;
	}

	public String getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(String finishTime) {
		this.finishTime = finishTime;
	}

	public String getInTime() {
		return inTime;
	}

	public void setInTime(String inTime) {
		this.inTime = inTime;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getOver70() {
		return over70;
	}

	public void setOver70(String over70) {
		this.over70 = over70;
	}

	public String getScore() {
		return score;
	}

	public void setScore(String score) {
		this.score = score;
	}

	public boolean isAvail() {
		return avail;
	}

	public void setAvail(boolean avail) {
		this.avail = avail;
	}

	public String getCheckResult1() {
		return checkResult1;
	}

	public void setCheckResult1(String checkResult1) {
		this.checkResult1 = checkResult1;
	}

	public String getCheckResult2() {
		return checkResult2;
	}

	public void setCheckResult2(String checkResult2) {
		this.checkResult2 = checkResult2;
	}
	
	
	
}
